package cn.dzhang.spring.web.controler;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.dzhang.spring.model.Account;
import cn.dzhang.spring.service.AccountService;
import cn.dzhang.spring.web.model.AccountsWrap;

@Controller
@RequestMapping("/accounts")
public class AccountController {
	@Resource
	private AccountService service;

	@RequestMapping("/new")
	public ModelAndView create(RedirectAttributes attributes) {
		Account account = new Account();
		ModelAndView mav = new ModelAndView("accounts/new");
		mav.addObject(account);
		return mav;
	}

	@RequestMapping(method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Map<String, Object> delete(
			@RequestParam(required = true, value = "id") String id) {
		Map<String, Object> result = new HashMap<String, Object>();
		boolean status = service.deleteById(id);
		result.put("status", status);

		String message = "";
		if (status) {
			message = "删除成功";
		} else {
			message = "删除失败，有可能此数据在数据库中不存在，请核对后在删除： id = " + id;
		}
		result.put("message", message);

		return result;
	}

	@RequestMapping("/edit/{id}")
	public ModelAndView edit(@PathVariable String id) {
		Account account = service.findById(id);
		ModelAndView mav = new ModelAndView("accounts/edit");
		mav.addObject(account);
		return mav;
	}

	@RequestMapping(method = { RequestMethod.PUT, RequestMethod.PATCH })
	public ModelAndView update(@ModelAttribute @Valid Account account,
			BindingResult result) {
		if (result.hasErrors()) {
			return new ModelAndView("redirect:accounts/edit/" + account.getId());
		}

		return saveOrUpdate(account);
	}

	protected ModelAndView saveOrUpdate(Account account) {
		ModelAndView mav = new ModelAndView("redirect:accounts");
		Account a = service.saveOrUpdate(account);
		mav.addObject(a);
		return mav;
	}

	@RequestMapping(method = RequestMethod.POST)
	public ModelAndView save(@ModelAttribute @Valid Account account,
			BindingResult result, RedirectAttributes attributes) {
		if (result.hasErrors()) {
			ModelAndView view = new ModelAndView("redirect:accounts/new");
			view.addObject(account);
			attributes.addAttribute(account);
			return view;
		}

		return saveOrUpdate(account);
	}

	@RequestMapping({ "", "/" })
	public ModelAndView index() {
		List<Account> accounts = service.findAll();
		ModelAndView mav = new ModelAndView("accounts/index");
		mav.addObject("accounts", new AccountsWrap(accounts));
		return mav;
	}

	@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE, value = { "",
			"/" })
	@ResponseBody
	public List<Account> indexJSON() {
		return service.findAll();
	}

	@RequestMapping(value = "/{id}")
	public String details(Model model, @PathVariable String id,
			RedirectAttributes attributes) {
		model.addAttribute("account", service.findById(id));
		return "accounts/details";
	}

	@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE, value = "/{id}")
	@ResponseBody
	public Account detailsJSON(@PathVariable String id) {
		return service.findById(id);
	}
}
